//------------------------------------------------------Fig2_3_marginsplot.do
// Fig2 and 3 Marginsplot
// --------------------------------------------------------------------------
version 15 // ensure compatibility
clear all // start in a clean state
cls // clear the results window
set more off // show all output, dont wait if screen full
set seed 12345 // see note below
set rmsg on // displays how time to execute command s
cd "C:\Users\wachb\OneDrive - UvA\WInequality\Replicate_Analysis"
capture log close // closes any log file if currently open
log using Fig2_3_marginsplot, replace text

//open the data
use "C:\Users\wachb\OneDrive - UvA\WInequality\Replicate_Analysis\countryxyzcorr.dta"

//label all the vars
label var meanUDS "Unified Democracy Score"
label var RGTQ "Social Trust"
label var MeanGiniSWIID "Gini income"
label var MeanGDP "Gross Domestic Product"
label var GiniWealth "Gini-wealth"
label var prepr "Trust in Representative Institutions"
label var pimpl "Trust in Implementing Institutions"
label var z_meanUDS "Unified Democracy Score"
label var z_logRGTQ "Social Trust"
label var z_MeanGiniSWIID "Gini-income"
label var z_logMeanGDP "Gross Domestic Product"
label var z_GiniWealth "Gini wealth"
label var z_prepr "Trust in Representative Institutions"
label var z_pimpl "Trust in Implementing Institutions"
label var t "Time Interval"
label var z_prepr_wg "Trust in Representative Institutions (excl. item on government)"

order ISO3166_2-logRGTQ GTQmis preprimplmis prepr_wg GTQmis preprimplmis ///
z_GiniWealth-z_meanUDS z_logMeanGDP z_logRGTQ z_prepr z_pimpl z_prepr_wg

//Fig 1 & Fig 2
*----------------------------interaction plots---------------------------------------
//interactions: gini-income trust institutions; control: giniwealth 
local intterms1 "c.z_MeanGiniSWIID##c.z_logMeanGDP c.z_MeanGiniSWIID##c.z_meanUDS"
local control1 z_GiniWealth
local intterms2 "c.z_GiniWealth##c.z_logMeanGDP c.z_GiniWealth##c.z_meanUDS"
local control2 z_MeanGiniSWIID
local AME1 "dydx(z_MeanGiniSWIID) at (z_meanUDS=(-1.99 (0.01) 1.99)) vsquish"
local AME4 "dydx(z_GiniWealth) at (z_logMeanGDP=(-1.99 (0.01) 1.99)) vsquish"

//initially/*scheme(sj)*/
/*local margopt1 "recastci(rarea) ciopts(fintensity(30)) scheme(sj) recast(line) ytitle("") title ("") subtitle("weighted")"*/ 
local margopt2 "recastci(rarea) ciopt(color(%20)) scheme(plottig) recast(line) plotopts(lw(thin)) xtitle("Unified Democracy Score", size(vlarge)) ytitle("") title("") subtitle("")"
local margopt3 "recastci(rarea) ciopt(color(%20)) scheme(plottig) recast(line) plotopts(lw(thin)) xtitle("Gross Domestic Product", size(vlarge)) ytitle("") ylabel(, nolabel notick) title("")"

foreach var of varlist (z_prepr z_pimpl) {
	quie reg `var' `control1' `intterms1' i.t, robust cluster(ISO3166_1_num1)
	quie margins, `AME1' 
	quie marginsplot, `margopt2' name(`var'1, replace)
	quie reg `var' `control2' `intterms2' i.t, robust cluster(ISO3166_1_num1)
	quie margins, `AME4' 
	quie marginsplot, `margopt3' name(`var'2, replace)
} 

foreach var of varlist (z_prepr z_pimpl) {
	local properties1 iscale(0.65) scheme(sj) imargin(0 0 0 0) ycommon title("
	local properties2 (with 95% CI)", size(medium))
	local properties3 subtitle("A. Gini income                                        B. Gini Wealth", size(medium) position (11)) 
	local name = subinstr("`var'", "z_", "",.)
	local name2 = subinstr("`name'", "prepr", "2_prepr",.)
	local name3 = subinstr("`name2'", "pimpl", "3_pimpl",.)
	cd "C:\Users\wachb\OneDrive - UvA\WInequality\Replicate_Analysis"
	graph combine `var'1 `var'2, `properties1' `: var label `var'' `properties2' `properties3' saving(Fig`name3', replace)
}
graph close
